package retrofitdemo.com.night.activity;

import android.content.Context;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import com.lzy.okgo.callback.StringCallback;

import java.io.IOException;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import okhttp3.Call;
import okhttp3.Response;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.bean.FloorMoreImageBean;
import retrofitdemo.com.night.bean.FloorMoreImageDescBean;
import retrofitdemo.com.night.databinding.ActivityFloorMoreImageDescBinding;
import retrofitdemo.com.night.utils.GlideUtil;
import retrofitdemo.com.night.utils.GsonUtil;
import retrofitdemo.com.night.utils.HTTPAPI;
import retrofitdemo.com.night.utils.LogUtil;

/**
 * Created by 86178 on 2020/3/12.
 * author mogen
 * 用心，成就一切。
 */
public class FloorMoreImageDescActivity extends BaseActivity {

    String module_floorId ;
    ActivityFloorMoreImageDescBinding binding;
    FloorMoreImageDescBean bean;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        binding = DataBindingUtil.setContentView(this, R.layout.activity_floor_more_image_desc );
        binding.setActivity( this );
        initView();
    }

    private void initView() {
        setBack();
        setTitleText( "展品详情" );
        module_floorId = getIntent().getStringExtra( "module_floorId" );

        HTTPAPI.getInstance().getFloorMoreImageDesc("2", new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e( "获取楼层成功-->"+ s );
                try {
                    bean = GsonUtil.GsonToBean( s, FloorMoreImageDescBean.class );
                    GlideUtil.setRoundUriMethod( FloorMoreImageDescActivity.this,bean.getData().getImg(),
                            binding.ivModule,15);
                    binding.setBean( bean.getData() );
                    startPlayMusic(bean.getData().getJiangjie());
                }catch (Exception e){

                }
            }
        });


    }


    public void startPlayMusic(final String url) {
        try {
            //1 初始化语音
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayer();
            } else {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                mediaPlayer.reset();
            }
            //网络地址
            Uri uri = Uri.parse(url);
            mediaPlayer.setDataSource(this, uri);

            //设置播放环境
            // 设置音量，参数分别表示左右声道声音大小，取值范围为0~1
            mediaPlayer.setVolume(0.9f, 0.9f);
            // 设置是否循环播放
            mediaPlayer.setLooping(false);
            mediaPlayer.prepareAsync();
            LogUtil.e("设置mp3，开始准备");

            //播放出错监听
            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                    startPlayMusic(url);
                    return false;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        setMediaPlayerStatus();
    }

    public void setMediaPlayerStatus() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}
